package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public User findByUsernameAndPassword(String username, String password) throws EmptyResultDataAccessException {
        System.out.println("userDao.findByUsernameAndPassword...");
        User user = jdbcTemplate.queryForObject("select * from t_user where username=? and password=?",new BeanPropertyRowMapper<User>(User.class),username,password);
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> userList = jdbcTemplate.query("select * from sys_user",new BeanPropertyRowMapper<User>(User.class));
        return userList;
    }

    @Override
    public Long insert(User user) {
        //jdbcTemplate.update("insert into values(?,?,?,?,?)",new BeanPropertyRowMapper<User>(User.class),null,user.getUsername(),user.getPassword(),user.getEmail(),user.getTelephone());
        PreparedStatementCreator psc = new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement preparedStatement = con.prepareStatement("insert into sys_user values(?,?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
                preparedStatement.setObject(1,null);
                preparedStatement.setObject(2,user.getUsername());
                preparedStatement.setObject(3,user.getPassword());
                preparedStatement.setObject(4,user.getEmail());
                preparedStatement.setObject(5,user.getTelephone());
                return preparedStatement;
            }
        };
        GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(psc,keyHolder);
        Long userId = keyHolder.getKey().longValue();
        return userId;
    }

    @Override
    public void saveUserRoleRel(Long userId, Long[] roleIds) {
        for(Long roleId : roleIds) {
            jdbcTemplate.update("insert into sys_user_role values(?,?)",userId, roleId);
        }
    }

    @Override
    public void delete(Long userId) {
        jdbcTemplate.update("delete from sys_user where id=?",userId);
    }

    @Override
    public void deleteUserRoleRel(Long userId) {
        jdbcTemplate.update("delete from sys_user_role where userId=?",userId);
    }

    @Override
    public void save() {
        System.out.println("UserDao.save...");
    }
}
